<?php

namespace app\admin\controller;

use app\admin\model\PayGoodsModel;
use cmf\controller\AdminBaseController;
use think\Request;

class PayGoodsController extends AdminBaseController
{
    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function add()
    {
        $rule = PayGoodsModel::where('type',PayGoodsModel::TYPE_VIP)->all();
        $this->assign('rule',$rule);

        return $this->fetch();
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request,PayGoodsModel $model)
    {
        $data = $request->param();

        for ($i=0;$i<count($data['price']);$i++){

            $rule = ['type' => PayGoodsModel::TYPE_VIP ,'price' => $data['price'][$i] ,'pay_name' => $data['pay_name'][$i]];
            $res = $model->allowField(true)->create($rule);
        }

        $this->success('添加成功!');
    }



    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request,PayGoodsModel $model)
    {
        $data = $request->param();

        for ($i=0;$i<count($data['price']);$i++){

            $rule = ['price' => $data['price'][$i] ,'pay_name' => $data['pay_name'][$i]];
            $res = $model->allowField(true)->update($rule,['id'=>$data['id'][$i]]);
        }
        $this->success('保存成功!');
    }


}
